package com.tonann.app.service.impl;

import com.tonann.app.domain.AppFault;
import com.tonann.app.domain.AppFaultHandle;
import com.tonann.app.domain.AppFaultHandleRecord;
import com.tonann.app.mapper.AppFaultHandleMapper;
import com.tonann.app.service.IAppFaultHandleRecordService;
import com.tonann.app.service.IAppFaultHandleService;
import com.tonann.app.service.IAppFaultService;
import com.tonann.common.core.domain.entity.SysUser;
import com.tonann.common.core.redis.RedisCache;
import com.tonann.common.utils.DateUtils;
import com.tonann.common.utils.SecurityUtils;
import com.tonann.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * 故障处理Service业务层处理
 *
 * @author tonann
 * @date 2022-10-15
 */
@Service
public class AppFaultHandleServiceImpl implements IAppFaultHandleService {
    @Autowired
    private AppFaultHandleMapper appFaultHandleMapper;

    @Autowired
    private IAppFaultService faultService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IAppFaultHandleRecordService faultHandleRecordService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询故障处理
     *
     * @param handleId 故障处理主键
     * @return 故障处理
     */
    @Override
    public AppFaultHandle selectAppFaultHandleByHandleId(Long handleId) {
        return appFaultHandleMapper.selectAppFaultHandleByHandleId(handleId);
    }

    /**
     * 查询故障处理列表
     *
     * @param appFaultHandle 故障处理
     * @return 故障处理
     */
    @Override
    public List<AppFaultHandle> selectAppFaultHandleList(AppFaultHandle appFaultHandle) {
        return appFaultHandleMapper.selectAppFaultHandleList(appFaultHandle);
    }

    /**
     * 新增故障处理
     *
     * @param appFaultHandle 故障处理
     * @return 结果
     */
    @Override
    public int insertAppFaultHandle(AppFaultHandle appFaultHandle) {
        appFaultHandle.setCreateTime(DateUtils.getNowDate());
        return appFaultHandleMapper.insertAppFaultHandle(appFaultHandle);
    }

    /**
     * 修改故障处理
     *
     * @param appFaultHandle 故障处理
     * @return 结果
     */
    @Override
    public int updateAppFaultHandle(AppFaultHandle appFaultHandle) {
        appFaultHandle.setUpdateTime(DateUtils.getNowDate());
        AppFaultHandleRecord faultHandleRecord = new AppFaultHandleRecord();
        faultHandleRecord.setHandleId(appFaultHandle.getHandleId());
        faultHandleRecord.setHandleRecord(appFaultHandle.getHandleRecord());
        faultHandleRecord.setUpdateBy(SecurityUtils.getUsername());
        faultHandleRecord.setCreateBy(SecurityUtils.getUsername());
        faultHandleRecord.setCreateTime(new Timestamp(new Date().getTime()));
        faultHandleRecord.setUpdateTime(new Timestamp(new Date().getTime()));
        faultHandleRecord.setUserId(SecurityUtils.getUserId());
        faultHandleRecordService.insertAppFaultHandleRecord(faultHandleRecord);
        if (appFaultHandle.getIsChecked().equals("1")){
            AppFault appFault = faultService.selectAppFaultByFaultId(appFaultHandle.getFaultId());
            redisCache.deleteObject("app_fault_list::device_id::" + appFault.getDeviceId());
        }
        return appFaultHandleMapper.updateAppFaultHandle(appFaultHandle);
    }

    /**
     * 批量删除故障处理
     *
     * @param handleIds 需要删除的故障处理主键
     * @return 结果
     */
    @Override
    public int deleteAppFaultHandleByHandleIds(Long[] handleIds) {
        return appFaultHandleMapper.deleteAppFaultHandleByHandleIds(handleIds);
    }

    /**
     * 删除故障处理信息
     *
     * @param handleId 故障处理主键
     * @return 结果
     */
    @Override
    public int deleteAppFaultHandleByHandleId(Long handleId) {
        return appFaultHandleMapper.deleteAppFaultHandleByHandleId(handleId);
    }

    @Override
    public void updateByModbus(AppFaultHandle resources) {
        resources.setUpdateTime(new Timestamp(new Date().getTime()));
        appFaultHandleMapper.updateAppFaultHandle(resources);
//        updateAllSameFaultByModbus(resources);
    }

    @Override
    public AppFaultHandle selectAppFaultHandleByFaultId(Long faultId) {
        return appFaultHandleMapper.selectAppFaultHandleByFaultId(faultId);
    }

    @Override
    public List<AppFaultHandle> selectAppFaultHandleListByFaultIds(Long[] faultIds) {
        return appFaultHandleMapper.selectAppFaultHandleListByFaultIds(faultIds);
    }

    private void updateAllSameFault(AppFaultHandle faultHandle, String record) {
        if (faultHandle.getIsChecked().equals("1")) {
            AppFaultHandle appFaultHandle = new AppFaultHandle();
            appFaultHandle.setFaultId(faultHandle.getFaultId());
            List<AppFaultHandle> faultHandleList = selectAppFaultHandleList(appFaultHandle);
            AppFault fault = faultService.selectAppFaultByFaultId(faultHandle.getFaultId());
            AppFault appFault = new AppFault();
            appFault.setSensorId(fault.getSensorId());
            List<AppFault> faultList = faultService.selectAppFaultList(appFault);
            for (AppFault faultDto : faultList) {
                AppFaultHandle handle = new AppFaultHandle();
                handle.setFaultId(faultDto.getFaultId());
                List<AppFaultHandle> faultHandleHistoryList = appFaultHandleMapper.selectAppFaultHandleList(handle);
                if (faultHandleHistoryList != null && faultHandleHistoryList.size() > 0) {
                    faultHandleHistoryList.sort(new Comparator<AppFaultHandle>() {
                        @Override
                        public int compare(AppFaultHandle o1, AppFaultHandle o2) {
                            if (o1.getCreateTime().getTime() > o2.getCreateTime().getTime()) {
                                return 1;
                            } else {
                                return -1;
                            }
                        }
                    });
                    AppFaultHandle faultHandleHistory = faultHandleHistoryList.get(0);
                    if (faultHandleHistory.getIsChecked().equals("0") && faultDto.getFaultRuleId() == faultHandle.getFault().getFaultRuleId()) {
                        faultHandleHistory.setIsChecked("1");
                        String userName = SecurityUtils.getUsername();
                        SysUser user = userService.selectUserByUserName(userName);
                        faultHandleHistory.setUserId(user.getUserId());
                        faultHandleHistory.setHandleRecord(record);
                        faultHandleHistory.setFault(faultDto);
                        faultHandleHistory.setFaultId(faultDto.getFaultId());
                        faultHandleHistory.setUpdateBy(userName);
                        faultHandleHistory.setUpdateTime(new Timestamp(new Date().getTime()));
                        updateAppFaultHandle(faultHandleHistory);
                        AppFaultHandleRecord faultHandleRecord = new AppFaultHandleRecord();
                        faultHandleRecord.setHandleId(faultHandleHistory.getHandleId());
                        faultHandleRecord.setHandleRecord(record);
                        faultHandleRecord.setUpdateBy(SecurityUtils.getUsername());
                        faultHandleRecord.setCreateBy(SecurityUtils.getUsername());
                        faultHandleRecord.setCreateTime(new Timestamp(new Date().getTime()));
                        faultHandleRecord.setUpdateTime(new Timestamp(new Date().getTime()));
                        faultHandleRecord.setUserId(user.getUserId());
                        faultHandleRecordService.insertAppFaultHandleRecord(faultHandleRecord);
                    }
                }
            }
        }
    }

    private void updateAllSameFaultByModbus(AppFaultHandle faultHandle) {
        if (faultHandle.getIsChecked().equals("1") && faultHandle.getFault() != null) {
            AppFault fault = faultService.selectAppFaultByFaultId(faultHandle.getFaultId());
            AppFault appFault = new AppFault();
            appFault.setSensorId(fault.getSensorId());
            List<AppFault> faultList = faultService.selectAppFaultList(appFault);
            for (AppFault faultDto : faultList) {
                AppFaultHandle appFaultHandle = new AppFaultHandle();
                appFaultHandle.setFaultId(faultDto.getFaultId());
                List<AppFaultHandle> faultHandleHistoryList = appFaultHandleMapper.selectAppFaultHandleList(appFaultHandle);
                for (AppFaultHandle faultHandleHistory : faultHandleHistoryList) {
                    if (faultHandleHistory != null && faultHandleHistory.getIsChecked().equals("0")
                            && faultDto.getFaultRuleId().equals(faultHandle.getFault().getFaultRuleId())) {
                        faultHandleHistory.setIsChecked("1");
                        SysUser user = userService.selectUserByUserName("admin");
                        faultHandleHistory.setUserId(user.getUserId());
                        faultHandleHistory.setHandleRecord("自动恢复");
                        faultHandleHistory.setFault(faultDto);
                        faultHandleHistory.setFaultId(faultDto.getFaultId());
                        faultHandleHistory.setUpdateBy(user.getUserName());
                        faultHandleHistory.setUpdateTime(new Timestamp(new Date().getTime()));
                        appFaultHandleMapper.updateAppFaultHandle(faultHandleHistory);
                        redisCache.deleteObject("app_fault_list::device_id::" + faultDto.getDeviceId());
                        AppFaultHandleRecord faultHandleRecord = new AppFaultHandleRecord();
                        faultHandleRecord.setHandleId(faultHandleHistory.getHandleId());
                        faultHandleRecord.setHandleRecord("自动恢复");
                        faultHandleRecord.setUpdateBy("Modbus");
                        faultHandleRecord.setCreateBy("Modbus");
                        faultHandleRecord.setCreateTime(new Timestamp(new Date().getTime()));
                        faultHandleRecord.setUpdateTime(new Timestamp(new Date().getTime()));
                        faultHandleRecord.setUserId(user.getUserId());
                        faultHandleRecordService.insertAppFaultHandleRecord(faultHandleRecord);
                    }
                }
            }
        }
    }


    @Override
    public void updateAllSameFaultsByModbus(List<AppFaultHandle> faultHandleList) {
                for (AppFaultHandle faultHandleHistory : faultHandleList) {
                    if (faultHandleHistory != null && faultHandleHistory.getIsChecked().equals("0")) {
                        faultHandleHistory.setIsChecked("1");
                        SysUser user = userService.selectUserByUserName("admin");
                        faultHandleHistory.setUserId(user.getUserId());
                        faultHandleHistory.setHandleRecord("自动恢复");
                        faultHandleHistory.setUpdateBy(user.getUserName());
                        faultHandleHistory.setUpdateTime(new Timestamp(new Date().getTime()));
                        appFaultHandleMapper.updateAppFaultHandle(faultHandleHistory);
                        AppFaultHandleRecord faultHandleRecord = new AppFaultHandleRecord();
                        faultHandleRecord.setHandleId(faultHandleHistory.getHandleId());
                        faultHandleRecord.setHandleRecord("自动恢复");
                        faultHandleRecord.setUpdateBy("Modbus");
                        faultHandleRecord.setCreateBy("Modbus");
                        faultHandleRecord.setCreateTime(new Timestamp(new Date().getTime()));
                        faultHandleRecord.setUpdateTime(new Timestamp(new Date().getTime()));
                        faultHandleRecord.setUserId(user.getUserId());
                        faultHandleRecordService.insertAppFaultHandleRecord(faultHandleRecord);
                    }
                }
    }

}
